package com.zmn.plat.business.impl.orderconfig.cancel;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zmn.common.utils.string.InitialUtil;
import com.zmn.plat.business.interfaces.orderconfig.cancel.OrderCancelCategoryBService;
import com.zmn.plat.manager.dictionary.ProductDict;
import com.zmn.plat.model.entity.orderconfig.cancel.OrderCancelCategory;
import com.zmn.plat.services.interfaces.orderconfig.cancel.OrderCancelCategoryService;

/**
 * @author tanbiao
 * @description
 * @since 2019/10/22 14:30
 * @modifier
 **/
@Service
@SuppressWarnings("Duplicates")
public class OrderCancelCategoryBServiceImpl implements OrderCancelCategoryBService {
    @Autowired
    private OrderCancelCategoryService orderCancelCategoryService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertAll(OrderCancelCategory orderCancelCategory, String creater) {
        // 检查名称是否重复
        OrderCancelCategory checkCategory = orderCancelCategoryService.findByName(orderCancelCategory.getName());
        if (checkCategory == null) {
            orderCancelCategory.setCreater(creater);
            orderCancelCategory.setCreateTime(new Date());
            orderCancelCategory.setUpdater(creater);
            orderCancelCategory.setUpdateTime(new Date());
            orderCancelCategory.setStatus(ProductDict.STATUS_ENABLE);
            orderCancelCategory.setFirstLetter(InitialUtil.getFristEnName(orderCancelCategory.getName()));
            orderCancelCategoryService.insert(orderCancelCategory);
            return ProductDict.FLAG_SUCCESS;
        }
        return ProductDict.FLAG_NAME_ERROR;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateAll(OrderCancelCategory orderCancelCategory, String updater) {
        // 检查名称是否重复
        OrderCancelCategory checkCategory = orderCancelCategoryService.findByName(orderCancelCategory.getName());
        if (checkCategory == null || checkCategory.getCategId().equals(orderCancelCategory.getCategId())) {
            OrderCancelCategory cancelCategory = orderCancelCategoryService.findByKey(orderCancelCategory.getCategId());
            cancelCategory.setUpdater(updater);
            cancelCategory.setUpdateTime(new Date());
            cancelCategory.setName(orderCancelCategory.getName());
            cancelCategory.setSort(orderCancelCategory.getSort());
            cancelCategory.setFirstLetter(InitialUtil.getFristEnName(orderCancelCategory.getName()));
            orderCancelCategoryService.updateByKey(cancelCategory);
            return ProductDict.FLAG_SUCCESS;
        }
        return ProductDict.FLAG_NAME_ERROR;
    }
}
